package cn.likun.kit.database.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import cn.likun.kit.validate.ValidateCommons;
/**
 * 数据分页类
 * <p>
 * 分页取值下标默认从 1 开始：<br>
 * 例：<br>
 * pageLimit = 10<br>
 * pageIndex = 1 > [startRow = 1 | endRow = 10 | startRowSubscript0 = 0 | endRowSubscript0 = 9]<br>
 * pageIndex = 2 > [startRow = 11 | endRow = 20 | startRowSubscript0 = 10 | endRowSubscript0 = 19]<br>
 * pageIndex = 3 > [startRow = 21 | endRow = 30 | startRowSubscript0 = 20 | endRowSubscript0 = 29]<br>
 * </p>
 */
public class Pagination<T> implements Serializable {

	private static final long serialVersionUID = 4201696292489996956L;

	private Integer pageIndex;
	private Integer pageLimit;
	private Integer pageTotal;
	private Integer rowTotal;
	/** startRow : 开始行,默认下标从1开始 */
	private Integer startRow;
	/** startRowSubscript0 : 开始行,默认下标从0开始*/
	private Integer startRowSubscript0;
	/** endRow : 结束行 , 默认下标从1开始*/
	private Integer endRow;
	/** endRowSubscript0 : 结束行 , 下标从0开始 */
	private Integer endRowSubscript0;
	
	private Boolean isFirst;
	private Boolean isLast;
	private List<T> results;
	
	/**
	 * 构造函数
	 * @param pageIndex 页数
	 * @param pageLimit 每页谁条数
	 */
	public Pagination(int pageIndex, int pageLimit) {
		this(pageIndex, pageLimit, pageIndex * pageLimit, null);
	}
	
	/**
	 * 构造函数
	 * @param pageIndex 页数 , 默认值为 1
	 * @param pageLimit 每页数据条数 , 默认值为 10
	 * @param rowTotal 数据总量
	 * @param results  结果集
	 */
	public Pagination(int pageIndex, int pageLimit, int rowTotal, List<T> results) {
		if (pageIndex == 0) {
			pageIndex = 1;
		}
		if (pageLimit == 0) {
			pageLimit = 10;
		}
		int pageTotal = 0;
		int startRow = 1;
		int startRowSubscript0 = 0;
		int endRow = 1;
		int endRowSubscript0 = 0;
		boolean isFirst = false;
		boolean isLast = false;
		if (rowTotal == 0) {
			pageIndex = 0;
			results = new ArrayList<T>();
			isFirst = true;
			isLast = true;
		} else {
			if (rowTotal % pageLimit == 0) {
				pageTotal = rowTotal / pageLimit;
			} else {
				pageTotal = rowTotal / pageLimit + 1;
			}
			if (pageIndex == 1) {
				isFirst = true;
			}
			if (pageIndex >= pageTotal) {
				isLast = true;
			}
			startRowSubscript0 = (pageIndex -1) * pageLimit;
			startRow = startRowSubscript0 + 1;
			if (isLast) {
				endRowSubscript0 = rowTotal - 1;
			} else {
				endRowSubscript0 = startRowSubscript0 + pageLimit - 1;
			}
			endRow = endRowSubscript0 + 1;
		}
		setPageIndex(pageIndex);
		setPageLimit(pageLimit);
		setPageTotal(pageTotal);
		setRowTotal(rowTotal);
		setStartRow(startRow);
		setStartRowSubscript0(startRowSubscript0);
		setEndRow(endRow);
		setEndRowSubscript0(endRowSubscript0);
		setIsFirst(isFirst);
		setIsLast(isLast);
	}
	
	
	
	public Integer getPageIndex() {
		return pageIndex;
	}
	public void setPageIndex(Integer pageIndex) {
		this.pageIndex = pageIndex;
	}
	public Integer getPageLimit() {
		return pageLimit;
	}
	public void setPageLimit(Integer pageLimit) {
		this.pageLimit = pageLimit;
	}
	public Integer getPageTotal() {
		return pageTotal;
	}
	public void setPageTotal(Integer pageTotal) {
		this.pageTotal = pageTotal;
	}
	public Integer getRowTotal() {
		return rowTotal;
	}
	public void setRowTotal(Integer rowTotal) {
		this.rowTotal = rowTotal;
	}
	public Integer getStartRow() {
		return startRow;
	}
	public void setStartRow(Integer startRow) {
		this.startRow = startRow;
	}
	public Integer getStartRowSubscript0() {
		return startRowSubscript0;
	}
	public void setStartRowSubscript0(Integer startRowSubscript0) {
		this.startRowSubscript0 = startRowSubscript0;
	}
	public Integer getEndRow() {
		return endRow;
	}
	public void setEndRow(Integer endRow) {
		this.endRow = endRow;
	}
	public Integer getEndRowSubscript0() {
		return endRowSubscript0;
	}
	public void setEndRowSubscript0(Integer endRowSubscript0) {
		this.endRowSubscript0 = endRowSubscript0;
	}
	public Boolean getIsFirst() {
		return isFirst;
	}
	public void setIsFirst(Boolean isFirst) {
		this.isFirst = isFirst;
	}
	public Boolean getIsLast() {
		return isLast;
	}
	public void setIsLast(Boolean isLast) {
		this.isLast = isLast;
	}
	public List<T> getResults() {
		return results;
	}
	public void setResults(List<T> results) {
		if (ValidateCommons.isEmpty(results)) {
			results = new ArrayList<T>();
		}
		this.results = results;
	}
	
	
	
}











































